{% extends 'ops/_nav_tab.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}

{% block tab %}
<li class="active">
    <a href="{% url 'ops:script-list' %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Script' %} </a>
</li>
<li>
    <a href="{% url 'ops:ansible-list' %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Ansible' %} </a>
</li>
<li>
    <a href="{% url 'ops:ansible-list' %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Salt' %} </a>
</li>
{% endblock %}

{% block table_container %}
<div class="uc pull-left m-r-5">
    <a href="{% url 'ops:script-create' %}" class="btn btn-sm btn-primary "> {% trans "Create Task" %} </a>
</div>
<table class="table table-striped table-bordered table-hover " id="script_list_table" >
    <thead>
    <tr>
        <th class="text-center">
            <input type="checkbox" id="check_all" class="ipt_check_all" >
        </th>
        <th class="text-center">{% trans 'Name' %}</th>
        <!--<th class="text-center">{% trans 'Run as' %}</th>-->
        <!--<th class="text-center">{% trans 'users' %}</th>-->
        <!--<th class="text-center">{% trans 'User groups' %}</th>-->
        <!--<th class="text-center">{% trans 'Asset' %}</th>-->
        <th class="text-center">{% trans 'Last update time' %}</th>
        <!--<th class="text-center">{% trans 'Execute result' %}</th>-->
        <th class="text-center">{% trans 'Comment' %}</th>
        <th class="text-center">{% trans 'Action' %}</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>
{% endblock %}



{% block custom_foot_js %}
<script src="{% static 'js/jquery.form.min.js' %}"></script>
<script>
function initTable() {
    var options = {
        ele: $('#script_list_table'),
        columnDefs: [
            {targets: 1, createdCell: function (td, cellData, rowData) {
                var detail_btn = '<a href="">' + cellData + '</a>';
                $(td).html(detail_btn.replace("{{ DEFAULT_PK }}", rowData.id));
                // if (rowData.id === 1 || rowData.username === "admin" || rowData.username === "{{ request.user.username }}") {
                //     $(td).html(detail_btn.replace("{{ DEFAULT_PK }}", rowData.id));
                // }
            }},

            {targets: 4, createdCell: function (td, cellData, rowData) {
                var excute_btn = '<a href="" class="btn btn-xs btn-primary btn-run">{% trans "Run" %}</a>'
                    .replace('{{ DEFAULT_PK }}', cellData);

                var update_btn = '<a href="{% url "ops:script-update" pk=DEFAULT_PK %}" class="btn btn-xs m-l-xs btn-info">{% trans "Update" %}</a>'
                    .replace('{{ DEFAULT_PK }}', cellData);

                var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_user_delete" data-uid="{{ DEFAULT_PK }}" data-name="99991938">{% trans "Delete" %}</a>'
                            .replace('{{ DEFAULT_PK }}', cellData)
                            .replace('99991938', rowData.name);
                $(td).html(excute_btn + update_btn + del_btn)
             }}
        ],
        ajax_url: '{% url "api-ops:script-list" %}',
        columns: [
            {data: function (){ return ""}}, {data: "task_name" },
            // {data: "users" },{data: "user_groups" },
            {data: "date_created"},
            {data: "comment" }, {data: "id" }
        ],
        };
    var table = glance.initServerSideDataTable(options);
    return table
}

$(document).ready(function(){
    initTable();
}).on('click', '.btn_user_delete', function(){
    var $this = $(this);
    var name = $(this).closest("tr").find(":nth-child(2)").children('a').html();
    // toastr.error($(this).closest("tr").find(":nth-child(2)").children('a').html());
    var uid = $this.data('uid');
    var the_url = '{% url "api-ops:script-detail" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", uid);
    objectDelete($this, name, the_url);
})
</script>
{% endblock %}
